﻿using System;
using System.Collections.Generic;
using System.IO;

namespace CommonEngine.Tools
{
    /// <summary>
    /// 文件操作
    /// </summary>
    public class DirectoryAct
    {
        /// <summary>
        /// 显示文件大小
        /// </summary>
        public static string GetSize(long length)
        {
            if (length.ToString().Length < 8)
            {
                return length / 1024 + " KB";
            }
            else
            {
                return length / 1024 / 1024 + " MB";
            }
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="FilePath">文件路径</param>
        /// <returns></returns>
        public static bool DeleteFile(string FilePath)
        {
            string FileSavePath = CommonOP.GetBinPath(FilePath);
            try
            {
                if (!File.Exists(FileSavePath))
                {
                    return true; ;
                }
                else
                {
                    File.Delete(FileSavePath);
                    return true;
                }
            }
            catch
            {
                return false;
            }

        }

        /// <summary>
        /// 删除指定文件夹下的所有文件（包括文件夹）
        /// </summary>
        /// <param name="strPath">文件夹路径</param>
        public static void DeleteFolder(string strPath)
        {
            if (Directory.Exists(strPath))
            {
                Directory.Delete(strPath, true);
            }
        }

        /// <summary>
        /// 返回文件列表
        /// </summary>
        public static IList<FileModel> GetFileList(string path)
        {
            if (!System.IO.Directory.Exists(path))
                return null;

            List<FileModel> FileList = new List<FileModel>();

            DirectoryInfo a = new DirectoryInfo(path);

            string[] di = System.IO.Directory.GetDirectories(path);
            FileInfo[] fi = a.GetFiles();

            foreach (FileInfo ft in fi)
            {
                FileModel fm = new FileModel();
                fm.CreationTime = ft.CreationTime;
                fm.LastAccessTime = ft.LastAccessTime;
                fm.Length = ft.Length;
                fm.Name = ft.Name;
                fm.LastWriteTime = ft.LastWriteTime;
                fm.Size = GetSize(ft.Length);
                fm.Ext = ft.Extension;
                FileList.Add(fm);
            }
            return FileList;
        }

        /// <summary>
        /// 返回日期文件名yyyyMMdd
        /// </summary>
        /// <returns>yyyyMMdd</returns>
        public static string GetDateName()
        {
            return DateTime.Now.ToString("yyyyMMdd");
        }

        /// <summary>
        /// 返回多层日期目录
        /// </summary>
        /// <param name="IsLayer">是否多层</param>
        /// <returns></returns>
        public static string GetDateName(bool IsLayer)
        {
            if (IsLayer)
            {
                DateTime dt = DateTime.Now;
                return dt.Year + "/" + dt.Month + "/" + dt.Day + "/";
            }
            else
                return DateTime.Now.ToString("yyyyMMdd");
        }

        /// <summary>
        /// 获取指定路径下的所有文件
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns></returns>
        public static IList<string> GetFileNameList(string path)
        {
            List<string> FileList = new List<string>();
            DirectoryInfo a = new DirectoryInfo(path);

            string[] di = System.IO.Directory.GetDirectories(path);
            FileInfo[] fi = a.GetFiles();

            foreach (FileInfo ft in fi)
            {
                FileList.Add(ft.FullName);
            }
            return FileList;
        }
        public static List<string> GetFileNameLists(string path, string allowExt)
        {
            List<string> FileLists = new List<string>();
            DirectoryInfo a = new DirectoryInfo(path);
            foreach (FileSystemInfo _a in a.GetFileSystemInfos())
            {
                if (_a is DirectoryInfo)
                {
                    FileLists = new List<string>(GetFileNameLists(_a.FullName, allowExt).ToArray());
                }
                else
                {
                    if (allowExt.IndexOf(_a.Extension + "|") != -1)
                    {
                        FileLists.Add(_a.FullName);
                    }
                }
            }
            return FileLists;
        }

        /// <summary>
        /// 获取指定路径下的指定类型文件
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="suffix">文件类型</param>
        public static IList<string> GetFileNameList(string path, string suffix)
        {
            List<string> FileList = new List<string>();
            DirectoryInfo a = new DirectoryInfo(path);

            string[] di = System.IO.Directory.GetDirectories(path);
            FileInfo[] fi = a.GetFiles();

            foreach (FileInfo ft in fi)
            {
                if (ft.Extension.ToUpper() == suffix)
                {
                    FileList.Add(ft.FullName);
                }
            }
            return FileList;
        }

        #region---创建文件夹---

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="Path">创建到路径</param>
        /// <param name="IsDateFolder">是否按日期创建文件夹</param>
        /// <param name="Status">是否创建成功(如果为false，将是文件已存在)</param>
        /// <returns>成功后返回文件夹路径，创建失败为null</returns>
        public static string CreateFolder(string Path, bool IsDateFolder, out bool Status)
        {
            Status = false;
            string FolderName = null;
            DateTime dateTime = DateTime.Now;
            string absPath = CommonOP.GetBinPath("/") + Path;
            string dir = dateTime.ToString(@"\\yyyyMMdd");
            if (IsDateFolder)
            {
                if (!System.IO.Directory.Exists(absPath + dir))
                {
                    System.IO.Directory.CreateDirectory(absPath + dir);
                    FolderName = absPath + dir;
                    Status = true;
                }
                FolderName = absPath + dir;
            }
            else
            {
                if (!System.IO.Directory.Exists(absPath))
                {
                    System.IO.Directory.CreateDirectory(absPath);
                    FolderName = absPath;
                    Status = true;
                }
                FolderName = absPath;
            }

            return FolderName;
        }

        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="Path">目录路径</param>
        /// <param name="IsDateFolder">是否自动创建</param>
        /// <returns></returns>
        public static string CreateFolder(string Path, bool IsDateFolder)
        {

            string FolderName = null;
            DateTime dateTime = DateTime.Now;
            string absPath = CommonOP.GetBinPath(Path);
            string dir = dateTime.ToString(@"\\yyyyMMdd");
            if (IsDateFolder)
            {
                if (!System.IO.Directory.Exists(absPath + dir))
                {
                    System.IO.Directory.CreateDirectory(absPath + dir);
                    FolderName = absPath + dir;

                }
                FolderName = absPath + dir;
            }
            else
            {
                if (!System.IO.Directory.Exists(absPath))
                {
                    System.IO.Directory.CreateDirectory(absPath);
                    FolderName = absPath;
                }
                FolderName = absPath;
            }

            return FolderName;
        }

        #endregion

        /// <summary>
        /// 文件是否存在
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <returns>是否存在</returns>
        public static bool FileExists(string filename)
        {
            return File.Exists(filename);
        }

        /// <summary>
        /// 根据文件路径，返回不包含文件名目录路径
        /// </summary>
        public static string GetFilePath(string PathStr)
        {
            return Path.GetDirectoryName(PathStr);
        }

        /// <summary>
        /// 根据文件路径，返回文件名和后缀名
        /// </summary>
        public static string GetFileName(string PathStr)
        {
            return Path.GetFileName(PathStr);
        }

        /// <summary>
        /// 返回文件后缀名
        /// </summary>
        public static string GetExtension(string FileName)
        {
            if (string.IsNullOrEmpty(FileName))
                return "";

            return FileName.Substring(FileName.LastIndexOf('.')).ToLower();
        }
    }

    /// <summary>
    /// 文件实体
    /// </summary>
    public class FileModel
    {
        /// <summary>
        /// 文件创建时间
        /// </summary>
        public virtual DateTime CreationTime { get; set; }
        /// <summary>
        ///  获取文件名
        /// </summary>
        public virtual string Name { get; set; }
        /// <summary>
        /// 获取当前文件的大小
        /// </summary>
        public virtual long Length { get; set; }

        /// <summary>
        /// 文件大小带单位
        /// </summary>
        public virtual string Size { get; set; }
        /// <summary>
        /// 上次访问当前文件或目录的时间
        /// </summary>
        public virtual DateTime LastAccessTime { get; set; }
        /// <summary>
        /// 上次写入当前文件或目录的时间
        /// </summary>
        public virtual DateTime LastWriteTime { get; set; }
        /// <summary>
        /// 文件类型
        /// </summary>
        public virtual string Ext { get; set; }
    }
}
